/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.room.maubinh;

import extension.game.room.Player;
import java.util.List;

/**
 *
 * @author daz
 */
public class MauBinhPlayer extends Player {

    List<Long> branchResult1;
    List<Long> branchResult2;
    List<Long> branchResult3;
    public static final int NORMAL_CARD = 1;
    private CardBranch[] cardBranchs;
    private int numberBranchs = 3;
    private long totalMoneyResult = 0;

    public void setTotalMoneyResult(long totalMoneyResult) {
        this.totalMoneyResult = totalMoneyResult;
    }

    public MauBinhPlayer(String name, long money, int seatPos) {
        super(name, money, seatPos);
        cardBranchs = new CardBranch[numberBranchs];
        for (int i = 0; i < numberBranchs; i++) {
            cardBranchs[i] = new CardBranch();
        }
    }

    public void setBranchResult1(List<Long> branchResult1) {
        this.branchResult1 = branchResult1;
    }

    public void setBranchResult2(List<Long> branchResult2) {
        this.branchResult2 = branchResult2;
    }

    public void setBranchResult3(List<Long> branchResult3) {
        this.branchResult3 = branchResult3;
    }

    public List<Long> getBranchResult1() {
        return branchResult1;
    }

    public List<Long> getBranchResult2() {
        return branchResult2;
    }

    public List<Long> getBranchResult3() {
        return branchResult3;
    }

    @Override
    public void resetCard() {
        listCardIds.clear();
        totalMoneyResult = 0;
    }

    public void addCard(int cardId, int type) {
        listCardIds.add(cardId);
        numberCardInHands++;
    }

    public void initCardBranh() {
        totalMoneyResult = 0;
        for (int i = 0; i < numberBranchs; i++) {
            cardBranchs[i].resetListCards();
        }
        for (int i = 0, l = listCardIds.size(); i < l; i++) {
            if (i < 3) {
                cardBranchs[2].addCard(listCardIds.get(i));
            } else if (i < 8) {
                cardBranchs[1].addCard(listCardIds.get(i));
            } else {
                cardBranchs[0].addCard(listCardIds.get(i));
            }
        }
        for (int i = 0; i < numberBranchs; i++) {
            cardBranchs[i].updateEval();
        }
    }

    public int getCardBranhRank(int branchId) {
        return cardBranchs[branchId - 1].getRank();
    }

    public int getCardBranchType(int branchId) {
        return cardBranchs[branchId - 1].getType();

    }

    public void setBranchMoneyWin(int branchId, long moneyResult) {
        totalMoneyResult += moneyResult;
        cardBranchs[branchId - 1].setMoneyResutl(moneyResult);
    }

    public long getTotalMoneyResult() {
        return totalMoneyResult;
    }

    public long getMoneyResult(int branchId) {
        return cardBranchs[branchId - 1].getMoneyResutl();
    }

    public List<Integer> getListHightLight(int branchId) {
        return cardBranchs[branchId - 1].getListcardHighLights();
    }

    public boolean exchangeCard(int card1, int branch1, int card2, int branch2) {
        CardBranch cardBranch1 = cardBranchs[branch1 - 1];
        CardBranch cardBranch2 = cardBranchs[branch2 - 1];
        if (cardBranch1.removeCard(card1)) {
            cardBranch1.addCard(card2);
            cardBranch1.updateEval();

        }
        if (cardBranch2.removeCard(card2)) {
            cardBranch2.addCard(card1);
            cardBranch2.updateEval();
        }
        return isRightOrder();
    }

    public boolean isRightOrder() {
        for (int i = numberBranchs - 1; i > 0; i--) {
            if (cardBranchs[i].getRank() > cardBranchs[i - 1].getRank()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public List<Integer> getListCardIds() {
        listCardIds.clear();
        for (int i = numberBranchs - 1; i >= 0; i--) {
            listCardIds.addAll(cardBranchs[i].getListCards());
        }
        return listCardIds;
    }
}
